<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/6/23
 * Time: 9:46 AM
 */
namespace app\backend\modules\sys\controllers;

use app\backend\controllers\BaseController;
use app\backend\modules\sys\services\NavService;
use app\common\models\NavMenu;
use Illuminate\Support\Facades\DB;

class NavMenuController extends BaseController
{
    public function index(){
        return view("backend.sys.navMenu.index");
    }
    public function getList(){
        $search = request()->search;
        $list = (new NavService())->getMenuList($search);
        return $this->successJson('',$list);
    }
    public function detail(){
        return view("backend.sys.navMenu.detail");
    }
    public function getDetail(){
        $id = intval(request()->id);
        if($id){
            $model = NavMenu::site()->select()
                ->where('id',$id)
                ->first();
            if (empty($model)) {
                return $this->errorJson('未找到数据');
            }
            $model->icon = fmt_media_url($model->icon);
            return $this->successJson('',$model);
        }else{
            return $this->errorJson('请传入导航ID');
        }
    }
    public function create(){
        $data = request()->params;
        $data['site_id'] = \YunCms::siteId();
        $data['pid'] = intval($data['pid']);
        $model = new NavMenu();
        $model->fill($data);
        $validator = $model->validator();
        if ($validator->fails()) {
            return $this->errorJson($validator->messages());
        } else {
            $parentPath = '0';
            if ($data['pid']) {
                $parentModel = NavMenu::find($data['pid']);
                if($parentModel['path']){
                    $parentPath = $parentModel['path'];
                }
            }
            DB::transaction(function() use ($model,$parentPath){
                $model->save();
                $model->path = $parentPath.'-'.$model->id;
                $model->save();
            });
            return $this->successJson();
        }
    }
    public function update(){
        $data = request()->params;
        $id = $data['id'];
        $model = NavMenu::find($id);
        if (empty($model)) {
            $this->errorJson('未找到数据');
        }
        $model->fill($data);
        $validator = $model->validator();
        if ($validator->fails()) {
            return $this->errorJson($validator->messages());
        } else {
            $oldPath = $model['path'];
            $path = '0-'.$id;
            if ($data['pid']) {
                $parentModel = NavMenu::find($data['pid']);
                if($parentModel['path']){
                    $path = $parentModel['path'].'-'.$id;
                }
            }
            $model->path = $path;
            $model->save();
            //修改子菜单path信息
            //如父菜单path=0-2-5，子菜单path=0-2-5-9
            $children = NavMenu::site()->select(['id','path'])
                ->where('path','like',$model['path'].'-%')
                ->get();
            foreach ($children as $child){
                //循环修改子菜单，一般不会太多
                $childPath = str_replace($oldPath . '-', $path . '-', $child['path']);
                NavMenu::where('id', $child['id'])->update(['path' => $childPath]);
            }
        }
        return $this->successJson();
    }
    public function delete(){
        $id = request()->id;
        $model = NavMenu::find($id);
        if(!$model){
            return $this->errorJson('未找到数据');
        }
        $childCount = NavMenu::site()
            ->where('pid',$id)
            ->count();
        if($childCount>0){
            return $this->errorJson('此分类有子类无法删除');
        }
        $model->delete();
        return $this->successJson();
    }
    public function changeStatus()
    {
        $id = request()->id;
        $model = NavMenu::site()->select()
            ->where('id',$id)
            ->first();
        if (empty($model)) {
            $this->errorJson('未找到数据');
        }
        $status=NavMenu::STATUS_NORMAL;
        if($model->status==NavMenu::STATUS_NORMAL){
            $status= NavMenu::STATUS_BAN;
        }
        $model->status = $status;
        $model->save();
        return $this->successJson();
    }
    /**
     * 检索菜单
     */
    public function query(){
        $kwd = trim(request()->kwd);
        $exceptId = intval(request()->except_id);
        $list = NavMenu::site()->select(['id','name'])
            ->where('status',NavMenu::STATUS_NORMAL);
        if($kwd){
            $list = $list->where('name','like','%'.$kwd.'%');
        }
        if($exceptId){
            $list = $list->where('id','<>',$exceptId);
        }
        $list = $list
            ->orderBy('display_order', 'desc')
            ->orderBy('id', 'desc')
            ->take(1000)
            ->get();
        return $this->successJson('',$list);
    }
}